<?php
	$filed= "inner";
	$chatfile= "chats/logs/$filed.html";
	if(!file_exists($chatfile) && !filesize($chatfile) > 0){
		$bgdated= months_inBG(date("d F : H:i"), 2);
		$handle = fopen($chatfile, "w");
		fwrite($handle, "");
	}
		$handle = fopen($chatfile, "r");
		$contents = fread($handle, filesize($chatfile));
		fclose($handle);
	if(isset($_COOKIE["PLAYON-chatPXs"])){
		$chatPXs=$_COOKIE["PLAYON-chatPXs"];
	} else $chatPXs=76;
?>

<link type="text/css" rel="stylesheet" href="chats/style.css" /> 
<div id="chatwrapper">
	<div id="chatlegend" class="smalltext">
		<b>Легенда: </b>
		<span class="textRED">членове на екипа</span>,
		<span class="textGREEN">доверени хора</span>,
		<span class="textYELLOW">новаци</span>
	</div>
	<div name="gamechatbox" id="gamechatbox" style="height:<?=$chatPXs?>px;">
		<?=$contents?>
	</div>	
	<form name="message" action="">
		<input type="text"   name="usermsg"   id="usermsg" placeholder="Искам да кажа..." class="tbox" />
		<input type="submit" name="submitmsg" id="submitmsg" value="Кажи" class="button" />
		<span style="float:right">
			<b>SizeY:</b>
			<select style="background:black;" id="sizeX" class="tbox">
				<option value="76">1x</option>
				<option value="142">2x</option>
				<option value="218">3x</option>
				<option value="284">4x</option>
				<option value="360">5x</option>
			</select>
		</span>
	</form>
</div>

<script type="text/javascript">
// jQuery Document
$(document).ready(function(){

	var gamechatbox= "#gamechatbox";
	scrollDown();
	setInterval (loadLog, 2500);	//Reload file every 2.5 seconds
	$("#sizeX").val("<?=$chatPXs?>");
	
	//If user submits the form
	$("#submitmsg").click(function(){	
		var clientmsg = $("#usermsg").val();
		$.post("chats/post.php?fileas=<?=$filed?>", {text: clientmsg});				
		$("#usermsg").attr("value", "");
		loadLog();
		scrollDown();
		return false;
	});
	
	//Load the file containing the chat log
	function loadLog(){	
		$.ajax({
			url: "chats/load.php?go=true&fileas=<?=$filed?>",
			cache: false,
			success: function(html){		
				$(gamechatbox).html(html); //Insert chat log into the #chatbox div
		  	},
		});
	}
	//Go to end of log
	function scrollDown(){
		$(gamechatbox).scrollTop($(gamechatbox)[0].scrollHeight);
	}
	
	$('#sizeX').change(function() {
		var pixelchange = $('#sizeX').val();  
		$(gamechatbox).css({"height": pixelchange+"px"});
		createCookie("PLAYON-chatPXs", pixelchange, 365);
	});
	
	function createCookie(name, value, days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
			var expires = "; expires=" + date.toGMTString();
		} else var expires = "";
		document.cookie = escape(name) + "=" + escape(value) + expires + "; path=/";
	}
	
});
</script>